package com.byedu.course.filter;

import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.focus.FocusUserService;
import com.byedu.domain.focus.FocusUserDO;
import com.byedu.util.CookieUtil;
import com.byedu.util.yunpian.util.StringUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * Created by Luo on 2017-08-24.
 */
public class LoginFilter implements Filter {
    public void destroy() {
        System.out.println("LoginFilter destroy");
    }

    private FocusUserService focusUserService;

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest request =(HttpServletRequest)req;
        HttpServletResponse response =(HttpServletResponse)resp;
//        String dd = "{\"password\":\"123456\",\"username\":\"罗洪州\"}";
//
//        FocusUserDO checkUser1 = JSON.parseObject(dd ,FocusUserDO.class);
//        System.out.println("---"+checkUser1);

        String url = request.getRequestURL().toString();
        if(url.contains("css") || url.contains("js") || url.contains("images")|| url.contains("file")){

        //如果发现是css或者js文件，直接放行
            chain.doFilter(request, response);
            return;
        }
        if(url.contains("login") ){
            //如果发现是登陆页面
            chain.doFilter(request, response);
            return;
        }
//        System.out.println("LoginFilter doFilter"+request.getRequestURL());
        //判断用户用户是否登陆
        FocusUserVO focusUserDO = (FocusUserVO)request.getSession().getAttribute("user");

        if(focusUserDO == null){
        //读取Cookie缓存
          String autoUser = CookieUtil.get("user",request);
            System.out.println(autoUser);
           if(autoUser != null){
            //获取是否选择自动登录
            //调用登陆业务校验是否合法
               String username = URLDecoder.decode(autoUser.split("-")[0], "UTF-8");
               String password =URLDecoder.decode(autoUser.split("-")[1], "UTF-8");

               FocusUserDO initUser = new FocusUserDO();
               initUser.setUsername(username);
               initUser.setPassword(password);
               FocusUserVO checkUser = focusUserService.getFocusUser(initUser);
//               System.out.println(checkUser);
               if(checkUser == null || checkUser.getId()==null){
                   //回到登陆页面
                   response.sendRedirect(request.getContextPath() + "/user/login");
                   return;
               }
               request.getSession().setAttribute("user",checkUser);
           }else{
               //回到登陆页面
               response.sendRedirect(request.getContextPath() + "/user/login");
               return;
           }

       }else{
            if(StringUtil.isNullOrEmpty(focusUserDO.getId())) {
                response.sendRedirect(request.getContextPath() + "/user/login");
                return;
            }
        }
           chain.doFilter(request, response);

    }

    public void init(FilterConfig config) throws ServletException {
        ServletContext servletContext = config.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        focusUserService = (FocusUserService)ctx.getBean("focusUserService");

        System.out.println("LoginFilter init");
    }

}
